#include <WinSock2.h>
#include <Windows.h>
#include "CAppInstance.h"
#include "CDirectories.h"
#include "CServerList.h"
#include "CEngine.h"

int WINAPI WinMain(
	HINSTANCE	hInstance		,
	HINSTANCE	hPrevInstance	,
	LPSTR		lpCmdLine		,
	int			nCmdShow
	)
{
	WSADATA			wsaData;
	CAppInstance	Instance(hInstance, nCmdShow);
	int				nRet ;

	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	WSAStartup(MAKEWORD(1, 1), &wsaData);

	if(!CDirectories::Initialize()) {
		MessageBox(NULL, "Unable to find Wonderland Online working directory",
			"Fatal Error", MB_ICONEXCLAMATION | MB_OK);	
		return -1;
	}

	if(!CServerList::Initialize()) {
		MessageBox(NULL, "Unable to load SERVER.INI",
			"Fatal Error", MB_ICONEXCLAMATION | MB_OK);
		return -1;
	}

	if(!CEngine::Initialize()) {
		MessageBox(NULL, "Bot Engine failed to initialize!", 
			"Fatal Error", MB_ICONEXCLAMATION | MB_OK);
		return -1;
	}

	if(!Instance.Create()) {
		MessageBox(NULL, "Bot Window Creation Failed!", 
			"Fatal Error", MB_ICONEXCLAMATION | MB_OK);
		return -1;
	}

	nRet = Instance.Run();

	CEngine::Uninitialize();
	CServerList::Uninitialize();
	CDirectories::Uninitialize();

	WSACleanup();

	return nRet;
}
